<?php

namespace app\service;

use app\model\User;
use Yurun\OAuthLogin\Weibo\OAuth2 as WeiboOAuth2;

class WeiboService
{
    public static ?WeiboService $_instance = null;

    /**
     * @return WeiboService|mixed
     */
    public static function instance(): ?WeiboService
    {
        if (!static::$_instance) static::$_instance = new self();
        return static::$_instance;
    }

    //    ========================================= Login ========================================

    /**
     * 获取微信登录配置
     *
     * @return array
     */
    public function getWeiboLoginConfig(): array
    {
        //OwenWeb
        return [
            'app_id'  => '1908694',
            'app_key' => '26baa5c2cca426',
        ];
    }

    /**
     * 获取微博登录url
     *
     * @return string
     */
    public function _weibo_login(): string
    {
        $weiboLoginConfig = $this->getWeiboLoginConfig();
        $appId            = $weiboLoginConfig['app_id'];
        $appKey           = $weiboLoginConfig['app_key'];

        $weiboOAuth = new  WeiboOAuth2($appId, $appKey, WEB_URL . '/login/weibo/');
        $url        = $weiboOAuth->getAuthUrl(WEB_URL . '/login/weibo/');
        $session    = request()->session();
        $session->set('QRCODE_WEIBO_STATE', $weiboOAuth->state);
        return $url;
    }

    public function _weibo_login_callback(string $token, int $weiboUid, string $ref = '', int $bindUid = 0): array
    {
        $weiboLoginConfig = $this->getWeiboLoginConfig();
        $appId            = $weiboLoginConfig['app_id'];
        $appKey           = $weiboLoginConfig['app_key'];

        $weiboOAuth               = new WeiboOAuth2($appId, $appKey, urlencode(WEB_URL));
        $weiboOAuth->isUseUnionID = true;
        try {
            $userInfo = $weiboOAuth->getUserInfo($token, $weiboUid);
            $unionid  = $userInfo['id'] ?? '';
            if (!$unionid) return ['code' => 400, 'msg' => 'unionid不存在'];
            if ($bindUid) {
                //绑定
                $where[] = ['status', '=', 1];
                $where[] = ['weibo_unionid', '=', $unionid];
                $where[] = ['id', '<>', $bindUid];
                $user    = User::where($where)->find();
                if ($user) return ['code' => 400, 'msg' => '该微博已被其他账号绑定'];

                $where1[] = ['id', '=', $bindUid];
                $user     = User::where($where1)->find();
                if (!$user) return ['code' => 400, 'msg' => '绑定用户不存在'];

                $data = [
                    'weibo_unionid' => $unionid,
                    'u_time'        => date('Y-m-d H:i:s'),
                ];
                User::updateDataCache($where1, $data);
            } else {
                $where2[] = ['weibo_unionid', '=', $unionid];
                $user     = User::where($where2)->find();
            }
            if (empty($user) && !$bindUid) $user = User::weibo_register($userInfo, $ref);
            return ['code' => 200, 'data' => $user];
        } catch (\InvalidArgumentException $exception) {
            return ['code' => 400, 'msg' => '未知错误'];
        }
    }

}